package com.hap.bank.controllers;

import com.hap.bank.dto.HandAccount;
import com.hap.bank.service.IHandAccountService;
import org.springframework.stereotype.Controller;
import com.hand.hap.system.controllers.BaseController;
import com.hand.hap.core.IRequest;
import com.hand.hap.system.dto.ResponseData;
import com.hap.bank.dto.HandAccountTransferHis;
import com.hap.bank.service.IHandAccountTransferHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindingResult;

import java.util.Date;
import java.util.List;

@Controller
public class HandAccountTransferHisController extends BaseController {

    @Autowired
    private IHandAccountTransferHisService service;

    @Autowired
    private IHandAccountService accountService;


    @RequestMapping(value = "/test/account/transfer/his/query")
    @ResponseBody
    public ResponseData query(HandAccountTransferHis dto, @RequestParam(defaultValue = DEFAULT_PAGE) int page,
                              @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) int pageSize, HttpServletRequest request) {
        IRequest requestContext = createRequestContext(request);
        return new ResponseData(service.select(requestContext, dto, page, pageSize));
    }

    @RequestMapping(value = "/test/account/transfer/his/submit")
    @ResponseBody
    public ResponseData update(@RequestBody List<HandAccountTransferHis> dto, BindingResult result, HttpServletRequest request) {
        getValidator().validate(dto, result);
        if (result.hasErrors()) {
            ResponseData responseData = new ResponseData(false);
            responseData.setMessage(getErrorMessage(result, request));
            return responseData;
        }
        IRequest requestCtx = createRequestContext(request);
        return new ResponseData(service.batchUpdate(requestCtx, dto));
    }

    @RequestMapping(value = "/test/account/transfer/his/remove")
    @ResponseBody
    public ResponseData delete(HttpServletRequest request, @RequestBody List<HandAccountTransferHis> dto) {
        service.batchDelete(dto);
        return new ResponseData();
    }

    @RequestMapping(value = "/test/account/amountTransfer")
    @ResponseBody
    public ResponseData amountTransfer(@RequestParam Long accountOut,
                                       @RequestParam Long accountIn,
                                       @RequestParam Long amount,
                                       HttpServletRequest request){
        IRequest requestContext = createRequestContext(request);
        // 处理转出的人
        HandAccountTransferHis outHis = new HandAccountTransferHis();
        outHis.setEmployAcctId(accountOut);
        outHis.setOppositeAcctId(accountIn);
        outHis.setType("转出");
        outHis.setAmount(amount);
        outHis.setTransDate(new Date());
        outHis.setObjectVersionNumber(1L);
        HandAccount outAccount = accountService.selectAccountByNum(String.valueOf(accountOut));
        outAccount.setAmount(outAccount.getAmount() - amount);
        accountService.updateByPrimaryKey(requestContext,outAccount);
        service.insert(requestContext,outHis);
        // 转入
        HandAccountTransferHis inHis = new HandAccountTransferHis();
        inHis.setEmployAcctId(accountIn);
        inHis.setOppositeAcctId(accountOut);
        inHis.setType("转入");
        inHis.setAmount(amount);
        inHis.setTransDate(new Date());
        inHis.setObjectVersionNumber(1L);
        HandAccount inAccount = accountService.selectAccountByNum(String.valueOf(accountIn));
        inAccount.setAmount(inAccount.getAmount() + amount);
        accountService.updateByPrimaryKey(requestContext,inAccount);
        service.insert(requestContext,inHis);
        return new ResponseData();
    }
}